package com.taokatao.cardmee.common.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.taokatao.cardmee.R;


@SuppressWarnings("unused")
public class AlertEditDialog extends Dialog {

    Params mParams;

    private AlertEditDialog(Context context, int theme) {
        super(context, theme);
    }

    public void setParams(Params p) {
        mParams = p;
    }

    public interface Callback {
        void onCompleted(AlertEditDialog dialog, String inputContent);

        void onCancel(AlertEditDialog dialog);
    }

    public static class Params {
        boolean hasShadow = true;
        boolean canCancel = true;
        Context mContext;
        String mTitle;
        String mOriginContent;
        String mInputHint;
        int maxLength;
        Callback mCallback;

        TextView tTitle, tCancel, tConfirm;
        EditText eInput;

    }

    public static class Builder {
        private Params p;

        public Builder setCancelable(boolean cc) {
            p.canCancel = cc;
            return this;
        }

        public Builder(Context context) {
            p = new Params();
            p.mContext = context;
        }

        public Builder setTitle(String title) {
            p.mTitle = title;
            return this;
        }

        public Builder setOriginContent(String content) {
            p.mOriginContent = content;
            return this;
        }

        public Builder setInputHint(String hint) {
            p.mInputHint = hint;
            return this;
        }

        public Builder setMaxLength(int maxLength) {
            p.maxLength = maxLength;
            return this;
        }

        public Builder setCallback(Callback callback) {
            p.mCallback = callback;
            return this;
        }

        public AlertEditDialog create() {
            final AlertEditDialog dialog = new AlertEditDialog(p.mContext, p.hasShadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);

            int dip1 = (int) (p.mContext.getResources().getDisplayMetrics().density + 0.5f);

            Window window = dialog.getWindow();
            window.getDecorView().setPadding(dip1 * 30, 0, dip1 * 30, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);

            View view = LayoutInflater.from(p.mContext).inflate(R.layout.dialog_alert_edit, null);
            p.tTitle = (TextView) view.findViewById(R.id.t_title);
            p.eInput = (EditText) view.findViewById(R.id.e_input);
            p.tCancel = (TextView) view.findViewById(R.id.t_cancel);
            p.tConfirm = (TextView) view.findViewById(R.id.t_confirm);

            p.tTitle.setText(p.mTitle);
            if (p.maxLength > 0) {
                p.eInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(p.maxLength)});
            }
            p.eInput.setHint(p.mInputHint);
            p.eInput.setText(p.mOriginContent);
            p.eInput.setSelection(TextUtils.isEmpty(p.mOriginContent) ? 0 : p.mOriginContent.length());
            p.eInput.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    p.eInput.setTag("edit");
                }
            });

            if (p.mCallback != null) {
                p.tConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        InputMethodManager imm = (InputMethodManager) p.eInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (p.eInput.getTag() != null && TextUtils.equals("edit", p.eInput.getTag().toString())) {
                            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                        }
                        p.mCallback.onCompleted(dialog, p.eInput.getText().toString().trim());
                    }
                });

                p.tCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        InputMethodManager imm = (InputMethodManager) p.eInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (p.eInput.getTag() != null && TextUtils.equals("edit", p.eInput.getTag().toString())) {
                            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                        }
                        p.mCallback.onCancel(dialog);
                    }
                });
            }

            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(p.canCancel);
            dialog.setCancelable(p.canCancel);
            dialog.setParams(p);
            return dialog;
        }
    }
}
